﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GONES.Tools.Common
{
    public class DataOperate
    {
        public static string GetHqlstrByExtFilter(IList<DataFilter> filters, string a)
        {
            if (filters == null || filters.Count == 0)
                return string.Empty;

            StringBuilder result = new StringBuilder();
            //type=string
            var stringList = from f in filters where f.Type == "string" select f;
            foreach (var i in stringList)
            {
                if(i.Comparison=="eq")
                {
                    result.Append(a + "." + i.Field + " = " + "'" + i.Value + "'" + " and ");
                    continue;
                }

                if(i.Comparison=="like")
                {
                    result.Append(a + "." + i.Field + " like " + "'%" + i.Value + "%'" + " and ");
                    continue;
                }

                if(i.Comparison=="llike")
                {
                    result.Append(a + "." + i.Field + " like " + "'" + i.Value + "%'" + " and ");
                    continue;
                }

                if(i.Comparison=="nq")
                {
                    result.Append(a + "." + i.Field + " <> " + "'" + i.Value + "'" + " and ");
                }

            }
            //type=boolean
            var booleanList = from f in filters where f.Type == "boolean" select f;
            foreach (var i in booleanList)
            {
                result.Append(a + "." + i.Field + "=" + i.Value + " and ");
            }
            //type=numeric
            var numericList = from f in filters where f.Type == "numeric" group f by f.Field into g select g;
            foreach (var i in numericList)
            {
                result.Append("( ");
                string iiStr = string.Empty;
                foreach (var ii in i)
                {
                    if (ii.Comparison == "in")
                    {
                        iiStr += a + "." + ii.Field + " in (" + ii.Value + ") and ";
                        continue;
                    }

                    iiStr += a + "." + ii.Field + GetComparison(ii.Comparison) + ii.Value + " and ";
                }
                result.Append(iiStr.Substring(0, iiStr.Length - 4));
                result.Append(" )");
                result.Append(" and ");
            }
            //type=date
            var dateList = from f in filters where f.Type == "date" group f by f.Field into g select g;
            foreach (var i in dateList)
            {
                result.Append("( ");
                string iiStr = string.Empty;
                foreach (var ii in i)
                {
                    iiStr += a + "." + ii.Field + GetComparison(ii.Comparison) + "'" + ii.Value +
                              "' and ";
                }
                result.Append(iiStr.Substring(0, iiStr.Length - 4));
                result.Append(" )");
                result.Append(" and ");
            }
            //type=list  :["1","2"]
            var listList = from f in filters where f.Type == "list" select f;
            foreach (var i in listList)
            {
                if (i.Comparison == "in")
                {
                    result.Append(a + "." + i.Field + " in " +
                                  i.Value.Replace("[", "( ").Replace("]", " )").Replace("\"", "'") + " and ");
                }
                else
                {
                    result.Append(a + "." + i.Field + " not in " +
                                  i.Value.Replace("[", "( ").Replace("]", " )").Replace("\"", "'") + " and ");
                }
            }

            return result.ToString().Substring(0, result.Length - 4);
        }

        private static string GetComparison(string comparison)
        {
            string res = string.Empty;
            switch (comparison)
            {
                case "lt":
                    res = "<";
                    break;
                case "gt":
                    res = ">";
                    break;
                case "eq":
                    res = "=";
                    break;
                case "nq":
                    res = "<>";
                    break;

            }
            return res;
        }
    }
}